package com.lun.swordtowardoffer2.c02;

public class MinSubArrayLen {

	public int minSubArrayLen(int k, int[] nums) {
		int p1 = 0, p2 = 0, sum = 0;
		int minLength = Integer.MAX_VALUE;

		while(p2 < nums.length) {
			sum += nums[p2];
			while(p1 <= p2 && sum >= k) {
				minLength = Math.min(minLength, p2 - p1 + 1);
				sum -= nums[p1++];
			}
			p2++;
		}
				
		return minLength == Integer.MAX_VALUE ? 0 : minLength;
	}
	
}
